package com.kangyi.user.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.PublicActivityPageQueryDTO;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.result.PageResult;
import com.kangyi.user.mapper.ActivitySignupMapper;
import com.kangyi.user.mapper.PublicActivityImgMapper;
import com.kangyi.user.mapper.PublicActivityMapper;
import com.kangyi.user.service.PublicActivityService;
import com.kangyi.vo.NursingHomeHotVO;
import com.kangyi.vo.PublicActivityDetailVO;
import com.kangyi.vo.PublicActivityImgVO;
import com.kangyi.vo.PublicActivityListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class PublicActivityServiceImpl implements PublicActivityService {

    @Autowired
    private PublicActivityMapper publicActivityMapper;

    @Autowired
    private PublicActivityImgMapper publicActivityImgMapper;

    @Autowired
    private ActivitySignupMapper activitySignupMapper;


    @Override
    public List<PublicActivityListVO> listPublicActivity() {
        // 1. 查询所有即将开始和进行中的活动
        List<PublicActivityListVO> resultList = publicActivityMapper.listPublicActivity();


        //1.1 保存现有活动id
        //获取所有活动的ID
        List<Integer> activityIds = resultList.stream()
                .map(PublicActivityListVO::getId)
                .collect(Collectors.toList());

        // 1.2 如果查询即将开始或进行中的活动数量不足3个，那么将结束的活动查询出来,进行补充
        while(resultList.size() < 3){
            List<PublicActivityListVO> publicActivityListVOS =  publicActivityMapper.getOverPublicActivity(new ArrayList<>(activityIds), 3 - resultList.size());
            for (PublicActivityListVO publicActivityListVO : publicActivityListVOS) {
                if (resultList.size() < 3 && !activityIds.contains(publicActivityListVO.getId())) {
                    resultList.add(publicActivityListVO);
                    activityIds.add(publicActivityListVO.getId());
                }
            }
            // 如果最新热门养老院列表为空，说明没有更多符合条件的养老院了，跳出循环
            if (publicActivityListVOS.isEmpty()) break;
        }

        if (CollUtil.isEmpty(resultList)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }


        // 3. 根据活动ID查询活动图片（只查询 sort = 1）
        List<PublicActivityImgVO> publicActivityImgs = publicActivityImgMapper.inquiryPublicActivityImgBatch(activityIds);

        // 4. 将图片添加到对应的活动中
        for (PublicActivityListVO activity : resultList) {
            PublicActivityImgVO activityImage = publicActivityImgs.stream()
                    .filter(img -> img.getPublicActivityId().equals(activity.getId()))
                    .findFirst()
                    .orElse(null);  // 如果没有图片，则为null
            activity.setPublicActivityImgVO(activityImage);  // 将图片添加到活动VO中
            activity.setTotal(activitySignupMapper.collectTotal(activity.getId()));
        }
        // 5. 返回结果
        return resultList;
    }

    @Override
    public PublicActivityDetailVO detailPublicActivity(Integer id) {

        PublicActivityDetailVO publicActivityDetailVO = publicActivityImgMapper.detailPublicActivity(id);
        if (publicActivityDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        List<PublicActivityImgVO> publicActivityImgVOList = publicActivityImgMapper.selectByPublicActivityId(id);

        publicActivityDetailVO.setPublicActivityImgVOList(publicActivityImgVOList);


        return publicActivityDetailVO;
    }
    @Override
    public PageResult pagePublicActivity(PublicActivityPageQueryDTO publicActivityPageQueryDTO) {
        PageHelper.startPage(publicActivityPageQueryDTO.getPage(),publicActivityPageQueryDTO.getPageSize());
        Page<PublicActivityListVO> page = publicActivityMapper.pageQuery(publicActivityPageQueryDTO);

        for (PublicActivityListVO publicActivityListVO : page) {
            Integer total = activitySignupMapper.collectTotal(publicActivityListVO.getId());
            publicActivityListVO.setTotal(total);
        }
        long total = page.getTotal();
        List<PublicActivityListVO> result = page.getResult();
        return new PageResult(total,result);
    }

}
